using System;
using System.IO;

namespace mp.Scanner
{
	public class PositioningReader : TextReader
	{		
		public PositioningReader (TextReader inner)
		{
			_inner = inner;
		}
		
		public override void Close(){
			_inner.Close();
		}
		
		public override int Peek() {
			return _inner.Peek();
		}
		
		public override int Read(){
			var c = _inner.Read();
			if(c >= 0)
				AdvancePosition((Char)c);
			return c;
		}
		
		private void AdvancePosition(Char c) {
			if (Environment.NewLine[_matched] == c) {
				_matched++;
				if(_matched == Environment.NewLine.Length) {
					_linePos++;
					_charPos = 0;
					_matched = 0;
				}
			}
			else {
				_matched = 0;
				_charPos++;
			}
		}
		
		
		#region properties
		
		private TextReader _inner;
		private int _linePos;
		public int LinePos { get { return _linePos; } }
		private int _charPos;
		public int CharPos { get { return _charPos; } }
		private int _matched;
				
		#endregion properties
	}
}

